package com.longrise.android.byjk.plugins.poster.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Util.StringUtils;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.poster.bean.ImageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/26.
 */

public class PosterListAdapter extends BaseQuickAdapter<ImageBean, BaseViewHolder> {

    private static final String TAG = "PosterListAdapter";

    List<ImageBean> mList = new ArrayList<>();
    private OnPosterItemClickListener mListener;

    public PosterListAdapter() {
        super(R.layout.item_rcv_poster);
    }


    public void setDatas(List<ImageBean> beans, boolean isLoadMore) {
        if (isLoadMore) {
            mList = new ArrayList<>();
        } else {
            mList.clear();
        }
        if (beans != null) {
            mList.addAll(beans);
        }
        if (isLoadMore) {
            addData(mList);
        } else {
            setNewData(mList);
        }
    }

    public void setOnRcvItemClickListener(OnPosterItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ImageBean item) {
        RelativeLayout rl_poster;
        ImageView iv_image, iv_tag;
        TextView tv_text, tv_share;
        rl_poster = helper.getView(R.id.rl_poster);
        iv_image = helper.getView(R.id.iv_image);
        iv_tag = helper.getView(R.id.iv_tag);
        tv_text = helper.getView(R.id.tv_text);
        tv_share = helper.getView(R.id.tv_share);
        tv_share.bringToFront();
        tv_text.setText(item.getAreaname());
        tv_share.setText(item.getShowsharenum() + "人已分享");
        if (!StringUtils.isEmpty(item.getIsnew()) && item.getIsnew().equals("1")) {
            iv_tag.setVisibility(View.VISIBLE);
        } else {
            iv_tag.setVisibility(View.GONE);
        }
        if (!StringUtils.isEmpty(item.getShowpicurl())) {
            Glide.with(mContext).load(item.getShowpicurl()).placeholder(R.drawable.bg_poster_default).into(iv_image);
        }
        rl_poster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(helper.getPosition());
                }
            }
        });
    }

    public interface OnPosterItemClickListener {
        void onClick(int position);
    }

}
